/* **********************************************************************************
*
* Copyright (c) Microsoft Corporation. All rights reserved.
*
* This source code is subject to terms and conditions of the Shared Source License
* for DSL Editor PowerToy. A copy of the license can be found in the License.htm file
* at the root of this distribution. If you can not locate the Shared Source License
* for DSL Editor PowerToy, please obtain a copy from: http://www.codeplex.com/dsltreegrideditor/Project/License.aspx.
* By using this source code in any fashion, you are agreeing to be bound by
* the terms of the Shared Source License for DSL Editor PowerToy.
*
* You must not remove this notice, or any other, from this software.
*
* **********************************************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

using VSTrid = Microsoft.VisualStudio.VirtualTreeGrid;

namespace DslEditorPowerToy.Controls.VirtualTreeGrid.Branches
{
    public abstract class DefaultNavigationBranchBase : BranchBase, IKeyNavigationBranch
    {
        #region Constructors
        protected DefaultNavigationBranchBase(VirtualTreeGridControl treeControl, Type branchDataItemType)
            : base(treeControl, branchDataItemType)
        {
        }
        #endregion

        #region IKeyNavigationBranch Members

        public ProcessKeyResult ProcessKeyPress(int row, int column, char keyPressed, Keys modifiers)
        {
            return new ProcessKeyResult(KeyAction.Process);
        }

        public ProcessKeyResult ProcessKeyDown(int row, int column, KeyEventArgs e)
        {
            //Initialise default result
            ProcessKeyResult processKeyResult = new ProcessKeyResult(KeyAction.Process);
            
            //Set the direction of navigation
            processKeyResult.Direction = (e.Modifiers != Keys.Shift) ? NavigationDirection.Right : NavigationDirection.Left;

            //Check the key for the current cell
            switch (e.KeyCode)
            {
                case Keys.F2:
                    //Enter label edit mode
                    processKeyResult.Action = KeyAction.Handle;
                    processKeyResult.StartLabelEdit = true;
                    break;

                case Keys.Tab:
                    //Navigate around all visible branches
                    processKeyResult.Action = KeyAction.Handle;
                    processKeyResult.LocalBranchNavigationOnly = false;
                    break;

                case Keys.Up:
                    //Navigate around all visible branches
                    if (false == e.Control)
                    {
                        processKeyResult.Action = KeyAction.Handle;
                        processKeyResult.LocalBranchNavigationOnly = false;
                        processKeyResult.Direction = NavigationDirection.Up;
                    }
                    break;

                case Keys.Down:
                    //Navigate around all visible branches
                    if (false == e.Control)
                    {
                        processKeyResult.Action = KeyAction.Handle;
                        processKeyResult.LocalBranchNavigationOnly = false;
                        processKeyResult.Direction = NavigationDirection.Down;
                    }
                    break;

                //case Keys.Escape:
                //    //Exit label edit mode
                //    processKeyResult.Action = KeyAction.Handle;
                //    processKeyResult.StartLabelEdit = false;
                //    break;
                
                default:
                    break;
            }

            return processKeyResult;
        }

        public abstract bool IsRowNewPlaceholderRow(int row);

        #endregion
    }
}
